package com.mayn.util;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.codecraft.xsoup.Xsoup;

/**
 * Xsoup解析html工具类 使用方法参考 https://github.com/code4craft/xsoup
 * http://webmagic.io/docs/zh/posts/ch4-basic-page-processor/xsoup.html
 * 
 * @author dadao
 *
 */
public class XsoupUtils {
	private static final Logger LOG = LoggerFactory.getLogger(XsoupUtils.class);

	public static List<String> GetXsoupList(String content, String regex) {
		List<String> list = new ArrayList<String>();
		try {
			if (content == null || content.equals("")) {
				LOG.error("content is null");
				return list;
			}
			if (regex == null || regex.equals("")) {
				LOG.error("regex is null");
				return list;
			}
			Document doc = Jsoup.parse(content);
			list = Xsoup.compile(regex).evaluate(doc).list();
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return list;
	}

	public static String GetXsoupStr(String content, String regex) {
		String ret = "";
		try {
			if (content == null || content.equals("")) {
				LOG.error("content is null");
				return ret;
			}
			if (regex == null || regex.equals("")) {
				LOG.error("regex is null");
				return ret;
			}
			Document doc = Jsoup.parse(content);
			ret = Xsoup.compile(regex).evaluate(doc).toString();
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return ret;
	}
}
